package chpt_06_02_mediator;

import javax.swing.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Ping
 * @date 2023/5/10 21:58
 **/
public class MediatorDemo {
    public static void main(String args[]) {
        ChatGroup chatGroup = new ChatGroup() ;
        Member normalMember = new NormalMember("normal") ;
        Member vipMember = new VIPMember("vip") ;
        chatGroup.addMember(normalMember);
        chatGroup.addMember(vipMember);
        normalMember.sendText(vipMember.getName(), "你好");
        normalMember.sendAudio(vipMember.getName(), "PICTURE.jpg");
        vipMember.sendText(normalMember.getName(), "你好");
        vipMember.sendAudio(normalMember.getName(), "IMAGE.jpg");
    }
}

/**
 * Mediator role:
 */
class ChatGroup {
    private Map<String, Member> memberMap = new ConcurrentHashMap<>() ;
    public void addMember(Member member) {
        memberMap.put(member.getName(), member) ;
        member.joinChatGroup(this);
        JButton button ;
    }
    public void transmitText(String from, String to, String text) {
        memberMap.get(to).recvText(from, text);
    }
    public void transmitAudio(String from, String to, String audio) {
        memberMap.get(to).recvAudio(from, audio);
    }


}

abstract class Member {
    protected String name ;
    protected ChatGroup chatGroup ;

    public Member(String name) {
        this.name = name;
    }

    public abstract void sendText(String to, String text) ;
    public abstract void sendAudio(String to, String audio) ;
    public abstract void recvText(String from, String text) ;
    public abstract void recvAudio(String from, String audio) ;
    public String getName() {
        return this.name ;
    }
    public void joinChatGroup(ChatGroup chatGroup) {
        this.chatGroup = chatGroup ;
    }
}

class NormalMember extends Member {
    public NormalMember(String name) {
        super(name);
    }

    @Override
    public void sendText(String to, String text) {
        System.out.println(String.format("[INFO] %s send to %s : %s", this.getName(), to, text));
        this.chatGroup.transmitText(this.getName(), to, text);

    }

    @Override
    public void sendAudio(String to, String audio) {
        System.out.println("[ERROR] cannot send audio, since no such right.");
    }

    @Override
    public void recvText(String from, String text) {
        System.out.println(String.format("[INFO] %s recv-from %s : %s", this.getName(),from,  text));
    }

    @Override
    public void recvAudio(String from, String audio) {
        System.out.println(String.format("[INFO] %s recv-audio-from %s : %s",this.getName(), from, audio));
    }
}

class VIPMember extends Member {
    public VIPMember(String name) {
        super(name);
    }

    @Override
    public void sendText(String to, String text) {
        System.out.println(String.format("[INFO] VIP %s send to %s : %s", this.getName(), to, text));
        this.chatGroup.transmitText(this.getName(), to, text);

    }

    @Override
    public void sendAudio(String to, String audio) {
        System.out.println(String.format("[INFO] VIP %s send-audio-to %s : %s", this.getName(), to, audio));
        this.chatGroup.transmitAudio(this.getName(), to, audio);

    }

    @Override
    public void recvText(String from, String text) {
        System.out.println(String.format("[INFO] VIP %s recv-from %s : %s", this.getName(),from,  text));
    }

    @Override
    public void recvAudio(String from, String audio) {
        System.out.println(String.format("[INFO] VIP %s recv-audio-from %s : %s",this.getName(), from, audio));
    }
}



